package ld.oauth.blog.config.plus;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import ld.oauth.blog.utils.CurrentUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class PropertyFillHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "creator", Integer.class,Objects.nonNull(CurrentUser.getCurrentUser())? Integer.valueOf(CurrentUser.getCurrentUser().getId().intValue()) : 0 );
        this.strictInsertFill(metaObject, "createDate", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updator", Integer.class, Integer.valueOf(CurrentUser.getCurrentUser().getId().intValue()));
        this.strictInsertFill(metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updator", Integer.class, Objects.nonNull(CurrentUser.getCurrentUser())? Integer.valueOf(CurrentUser.getCurrentUser().getId().intValue()) : 0 );
        this.strictUpdateFill(metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now());
    }
}
